﻿@{
    Layout = "~/Views/Shared/_CompactLayout.cshtml";
}

<table class="slider"><tr><td>Font size:</td><td style="width:100px;"><div id="slider" /></td></tr></table>

<div id="questions"></div>

<button id="submit">Submit My Answers</button>
<img id="loading" src="@Url.Content("~/Content/loading.gif")" alt="loading..." style="display:none;" />

<script type="text/javascript">
    $(function () {
        var questions = [];
        $('#submit').button({ icons: { primary: 'ui-icon-circle-check'} }).disableDuringAjax();
        $.post('@Url.Action("GetQuestions", "Question")', { surveyId: '@ViewBag.SurveyId' }, function (response) {
            questions = response;
            $tmpl('#questionsTemplate', questions, '#questions');
            $('.page').trigger('resize');
        }, 'json');
        $('#submit').click(function () {
            var answers = [];
            for (var i in questions) {
                var question = questions[i];
                var answer = {};
                var checkedItem = $('input[name=q_' + question.QuestionId + ']:checked');
                if (checkedItem != null && checkedItem.length > 0) {
                    answer.QuestionId = question.QuestionId;
                    answer.SelectedIndex = parseInt(checkedItem.attr('ss:index'), 10);
                    answers.push(answer);
                }
            }
            $.post('@Url.Action("SubmitAnswers", "Answer")',
                { surveyId: '@ViewBag.SurveyId', answersString: JSON.stringify(answers) },
                function (response) { window.location.assign('@Url.Action("Submitted", "Survey", new { id = ViewBag.SurveyId })'); });
        });

        $('#slider').slider({ value: 50 })
            .on('slide', function () { setTimeout(updateFontSize, 10); })
            .on('change', function () { setTimeout(updateFontSize, 10); })
        var updateFontSize = function () {
            var value = $('#slider').slider('value');
            var fontSize = 1 + (value - 50) * 0.01;
            $('#questions').add('#submit').css('font-size', fontSize + 'em');
            $('.page').trigger('resize');
        };
        $registerAjaxWaitingElement('img#loading');
    });
</script>

@{Html.RenderPartial("Templates/QuestionTemplates");}